package com.tybbank.Utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

/**
 * Created by Administrator on 2018/9/25 0025.
 */

public class MD5Utils {

    public static String md5Password(String password) {
        StringBuffer sb = new StringBuffer();
        // 得到一个信息摘要器
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(password.getBytes());
            // 把每一个byte做一个与运算 0xff
            for (byte b : result) {
                // 与运算
                int number = b & 0xff;
                String str = Integer.toHexString(number);
                if (str.length() == 1) {
                    sb.append("0");
                }
                sb.append(str);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static String getTime() {
       long time=System.currentTimeMillis();
    //将毫秒值转换为String类型数据         
         String time_stamp =String.valueOf(time);
        return time_stamp;
    }

    public static String Sign(List list) {
        String url = "TimeStamp=" + MD5Utils.getTime() + "&key=" + "F2%46#CD9DE8D@F1#CE29$15#02E";
        for (int i = 0; i < list.size(); i++) {
            url = list.get(i) + "&" + url;

        }
        String suburl = url.substring(0, url.length() - 1);
        logUtilssss.e("TAGjiam"+ suburl);
        StringBuffer sb = new StringBuffer();
        // 得到一个信息摘要器
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(suburl.getBytes());
            // 把每一个byte做一个与运算 0xff
            for (byte b : result) {
                // 与运算
                int number = b & 0xff;
                String str = Integer.toHexString(number);
                if (str.length() == 1) {
                    sb.append("0");
                }
                sb.append(str);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }


    /**
     *   * 微信支付签名算法sign
     *   *
     *   * @param characterEncoding
     *   * @param parameters
     *   * @return
     *   
     */
    public static String createSign(SortedMap<String, String> parameters) {
        String characterEncoding = "UTF-8";
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            @SuppressWarnings("rawtypes")
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v) && !"".equals(k)&& null !=k
                   ) {  // && !"key".equals(k)
                sb.append(k + "=" + v + "&");
            }
        }
        String sign = MD5Utils.md5Password(sb.substring(0,sb.length()-1)+"&key=0f4137ed1502b5045d6083aa258b5c42");
        return sign; //
    }

}

